using System;
using System.Security.Cryptography.X509Certificates;
using Cfg.Attributes;
using Cfg.ConfigValues;
using NUnit.Framework;
using NUnit.Mocks;

namespace Cfg.Test
{
    [TestFixture]
    public class PathTest_WithStaticFields
    {
        public class Conf
        {
            [Path]
            public static PathInfo P { get; set; }

            public class PathInfoTest
            {
                [Path]
                public static PathInfo Validators { get; set; }

                [Path]
                public static PathInfo Root { get; set; }

                [Path]
                public static PathInfo InvalidRoot { get; set; }
            }
        }

        [Test]
        public void GetPathInfoFromConfigFieldThatDoesNotExist()
        {
            Cfg.AppSettings.Init<Conf>();

            Console.WriteLine(Conf.P.FullPath);
        }

        [Test]
        public void GetPathInfo()
        {
            Cfg.AppSettings.Init<Conf>();

            Console.WriteLine(Conf.PathInfoTest.Validators.FullPath);
        }

        [Test]
        public void Root()
        {
            Cfg.AppSettings.Init<Conf>();

            Console.WriteLine(Conf.PathInfoTest.Root.FullPath);
        }

        [Test]
        public void InvalidRoot()
        {
            Cfg.AppSettings.Init<Conf>();

            Console.WriteLine(Conf.PathInfoTest.InvalidRoot.FullPath);
        }
    }

    [TestFixture]
	public class PathTest_WithInterface
	{
        public interface ITestConfig
        {
            PathInfo PathInfo { get; set; } 
        }

        [Test]
	    public void Test()
	    {
	        Cfg.AppSettings.Init<ITestConfig>();
	    }

        [Test]
        public void GetAbsoluteUrl()
        {
            var attr = new object[] { new PathAttribute(false, false) };

            var absolutePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\Release", "\\xxx").Replace("\\Debug", "\\xxx") + "\\x";
            Console.WriteLine(absolutePath);

            var vb = new ValueBuilder(typeof(PathInfo), attr, null, null, absolutePath);
            var value = (PathInfo)vb.GetValue();

            Console.WriteLine(value.Url);
            Console.WriteLine(value.RelativeUrl);

            Assert.AreEqual("..\\xxx\\x", value.RelativePath);
            
        }

		[Test]
		public void GetRelativePathFromAbsolute()
		{
			var attr = new object[] { new PathAttribute(false, false) };

			var absolutePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\Release", "\\xxx").Replace("\\Debug", "\\xxx") + "\\x";
			Console.WriteLine(absolutePath);

			var vb = new ValueBuilder(typeof(PathInfo), attr, null, null, absolutePath);
			var value = (PathInfo)vb.GetValue();

			Assert.AreEqual("..\\xxx\\x", value.RelativePath);
		}

		[Test]
		public void GetAbsolutePathFromRelative()
		{
			var attr = new object[] { new PathAttribute(false, false) };

			var relativePath = "x/y/z";
			var vb = new ValueBuilder(typeof(PathInfo), attr, null, null, relativePath);
			var value = (PathInfo)vb.GetValue();

			var absolutePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath.Replace(System.IO.Path.AltDirectorySeparatorChar, System.IO.Path.DirectorySeparatorChar));
			Console.WriteLine(absolutePath);

			Assert.AreEqual(absolutePath, value.FullPath);
		}
	}
}